﻿package com.tomvian.twodee.common.interactions
{
	import flash.display.MovieClip;
	import com.tomvian.twodee.common.interactions.Interaction;
	import com.tomvian.maths.Vector2D;
	
	/**
	 * Collecting
	 * @author Thomas Vian
	 */
	public class Collectables extends Interaction 
	{
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		public var collectables:Array;
		public var collectCallback:Function;
		
		//--------------------------------------------------------------------------
		//	
		// Getters / Setters
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//	
		//  Constructor
		//
		//--------------------------------------------------------------------------
		
		public function Collectables(collectables:Array, collectPoint:Vector2D = null, collectCallback:Function = null)
		{
			super(collectables, collectPoint);
			
			this.collectCallback = collectCallback;
		}
		
		//--------------------------------------------------------------------------
		//	
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		override public function interact():void 
		{
			if (enabled)
			{
				_testPoint.copy(actorMass.position);
				_testPoint.addMod(actorMass.velocity).addMod(interactionPoint);
				
				wasInteracting = interacting;
				interacting = false;
				
				for (var i:uint = 0, l:uint = interactiveObjects.length; i < l; i++)
				{
					var collectable:MovieClip = interactiveObjects[i];
					
					if (collectable.hitTestPoint(_testPoint.x, _testPoint.y, true))
					{
						interacting = true;
						
						collectable.gotoAndPlay(2);
						
						if (Boolean(collectCallback)) collectCallback(collectable);
						if (collectable.collect && collectable.collect is Function) collectable.collect();
						
						interactiveObjects.splice(i, 1);
						i--;
						l--;
					}
				}
			}
		}
	}
}